package banwood.com.xiongfasy.activity;


import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import banwood.com.xiongfasy.BASE.BaseActivity;
import banwood.com.xiongfasy.R;
import banwood.com.xiongfasy.bean.BillDetail;
import banwood.com.xiongfasy.utils.ToolsUtil;
import butterknife.BindView;
import butterknife.OnClick;
import cn.weipass.pos.sdk.IPrint;
import cn.weipass.pos.sdk.LatticePrinter;
import cn.weipass.pos.sdk.Weipos;
import cn.weipass.pos.sdk.impl.WeiposImpl;
import cn.weipass.pos.sdk.LatticePrinter.FontFamily;
import cn.weipass.pos.sdk.LatticePrinter.FontSize;
import cn.weipass.pos.sdk.LatticePrinter.FontStyle;


/*
 * 账单详情页面
 * */
public class BillDetailActivity extends BaseActivity {
    @BindView(R.id.bianhao)
    TextView bianhao;
    @BindView(R.id.liushui)
    TextView liushui;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.bill_money)
    TextView billMoney;
    @BindView(R.id.print_piao)
    Button printPiao;
    private String title;
    private BillDetail paramMap;
    private Intent intent;

    @Override
    protected int getLayoutId() {
        return R.layout.bill_detail;
    }

    @Override
    protected void initView() {
        intent = getIntent();
        if (intent != null) {
            title = intent.getExtras().getString("title");
            paramMap = intent.getExtras().getParcelable("param");
            if (title.equals("查询结果")){
                printPiao.setVisibility(View.GONE);
            }
            if (title.length() > 0 && !title.equals("")) {
                settitle(title);
            }
            if (paramMap != null) {
                bianhao.setText(MyApplication.getUserName());
                liushui.setText(paramMap.getIRspRef());
                time.setText(paramMap.getOrderDate()+"--"+paramMap.getOrderTime());
                billMoney.setText(paramMap.getOrderAmount() + "元");
            }
        }





    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.print_piao)
    public void printOnclick(View v){
        printPiao.setClickable(false);
        WeiposImpl.as().init(BillDetailActivity.this, new Weipos.OnInitListener() {
            @Override
            public void onInitOk() {
                LatticePrinter printer = WeiposImpl.as().openLatticePrinter();
                String mediumSpline = "";
                for (int i = 0; i <ToolsUtil.mediumSize; i++) {
                    mediumSpline += "-";
                }
                String biaoti="山东雄发商贸有限公司";
                int sizeTitle = ToolsUtil.largeSize - ToolsUtil.length(biaoti);
                // 文字居中需要在前面补足相应空格，后面可以用换行符换行
                String titleStr = getBlankBySize((int) (sizeTitle / 2d)) + biaoti;
                printer.printText(titleStr + "\n", FontFamily.SONG,
                        FontSize.LARGE, FontStyle.BOLD);

                printer.feed(1);

                String shophao="商户号：103881511100395";
                int sizeWebUrl = ToolsUtil.mediumSize - ToolsUtil.length(shophao);
                String webUrlStr = getBlankBySize((int) (sizeWebUrl / 2d)) + shophao;
                printer.printText(webUrlStr + "\n", FontFamily.SONG,
                        FontSize.MEDIUM, FontStyle.BOLD);

                int feedCount = 1;
                //进纸数，1代表进一行的高度
                printer.feed(feedCount);
                // 打印分割线
                printer.printText(mediumSpline + "\n", FontFamily.SONG,
                        FontSize.MEDIUM, FontStyle.BOLD);
                printer.feed(feedCount);

                String money="交易金额:"+paramMap.getOrderAmount()+"元";
                printer.printText(money + "\n", FontFamily.SONG,
                        FontSize.LARGE, FontStyle.BOLD);

                printer.feed(1);

                String time="交易日期:"+paramMap.getOrderDate()+"--"+paramMap.getOrderTime();
                printer.printText(time + "\n", FontFamily.SONG, FontSize.MEDIUM,
                        FontStyle.BOLD);

                printer.feed(1);

                String liiushuihao="订单号:"+paramMap.getOrderNo();
                printer.printText(liiushuihao + "\n", FontFamily.SONG, FontSize.MEDIUM,
                        FontStyle.BOLD);

                printer.feed(1);

                String caozuoyuan="操作员:"+MyApplication.getUserName();
                printer.printText(caozuoyuan + "\n", FontFamily.SONG, FontSize.MEDIUM,
                        FontStyle.BOLD);

                //最后进纸5行,方便撕纸
                printer.feed(5);
                printer.submitPrint();

                printer.setOnEventListener(new IPrint.OnEventListener() {
                    @Override
                    public void onEvent(int i, String s) {
                        String message=getPrintErrorInfo(i,s);
                        Toast.makeText(BillDetailActivity.this,message,Toast.LENGTH_SHORT).show();
                        printPiao.setClickable(true);
                    }
                });

            }



            @Override
            public void onError(String s) {

                Toast.makeText(BillDetailActivity.this,s,Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onDestroy() {

            }
        });

    }

    public static String getBlankBySize(int size) {
        String resultStr = "";
        for (int i = 0; i < size; i++) {
            resultStr += " ";
        }
        return resultStr;
    }


    public static String getPrintErrorInfo(int what, String info) {
        String message = "";
        switch (what) {
            case IPrint.EVENT_CONNECT_FAILD:
                message = "连接打印机失败";
                break;
            case IPrint.EVENT_CONNECTED:
                // Log.e("subscribe_msg", "连接打印机成功");
                break;
            case IPrint.EVENT_PAPER_JAM:
                message = "打印机卡纸";
                break;
            case IPrint.EVENT_UNKNOW:
                message = "打印机未知错误";
                break;
            case IPrint.EVENT_STATE_OK:
                //打印机状态正常
                break;
            case IPrint.EVENT_OK://
                // 回调函数中不能做UI操作，所以可以使用runOnUiThread函数来包装一下代码块
                // 打印完成结束
                message = "打印成功";
                break;
            case IPrint.EVENT_NO_PAPER:
                message = "打印机缺纸";
                break;
            case IPrint.EVENT_HIGH_TEMP:
                message = "打印机高温";
                break;
            case IPrint.EVENT_PRINT_FAILD:
                message = "打印失败";
                break;
        }
        return message;
    }


}
